package rpcserver

import (
	corev1 "gluenet/internal/agent/api/core/v1"
	rpcv1 "gluenet/internal/agent/api/rpc/v1"
	"gluenet/internal/agent/depend"
	"gluenet/internal/agent/driver/rpcserver"
	"gluenet/internal/agent/service/k8ssvc"
)

const (
	k8s = rpcv1.RPC_SERVICE_K8S
)

func init() {
	depend.Provide(NewK8sRouter)
	registerRouter(k8s, func(c rpcserver.IRoutes) {
		depend.Invoke(func(d *k8sRouter) {
			d.initRouter(c)
		})
	})
}

type k8sRouter struct {
	kubernetes k8ssvc.Service
}

func NewK8sRouter(svc *k8ssvc.Service) *k8sRouter {
	return &k8sRouter{*svc}
}

func (r *k8sRouter) initRouter(c rpcserver.IRoutes) {
	c.Func(`k8s.namespace.container.list`, r.ListContainerInNameSpace)
}

func (r *k8sRouter) ListContainerInNameSpace(c *rpcserver.Context) {
	meta := new(corev1.GluenetMeta)
	if err := c.BindJSON(meta); err != nil {
		return
	}

	result := r.kubernetes.ListContainers(meta)
	c.JSON(result)
}
